package P150;

/**
 * @author LiangMu
 * @version \$Id: P23.java, v 0.1, 2024年07月05日 14:29 LiangMu Exp $$
 */
public class P23 {

    public static boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        char[] chars = s.toCharArray();
        while (left < right) {
            char lc = chars[left];
            while (!isLegal(lc)) {
                ++left;
                if (left >= right) {
                    return true;
                }
                lc = chars[left];
            }
            char rc = chars[right];
            while (!isLegal(rc)) {
                --right;
                if (right <= left) {
                    return true;
                }
                rc = chars[right];
            }
            int l = lc <= 'Z' && lc >= 'A' ? lc - 'A' : lc - 'a';
            int r = rc <= 'Z' && rc >= 'A' ? rc - 'A' : rc - 'a';
            if (l != r) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }

    private static boolean isLegal(char c) {
        return (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a') || (c <= '9' && c >= '0');
    }

    public static void main(String[] args) {
       String s = "A man, a plan, a canal: Panama";
        System.out.println(isPalindrome(s));
    }

}
